//
// Unihan Input Method
//
// Copyright © 2002-2007 Sun Wah Linux Ltd.
//
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
// USA
//
// $Id: table_io.c 757 2007-05-10 08:19:31Z roger $
//

/* file : table_io.c */

#include <SWTable.h>

#include "table_io.h"

#include <string.h>
#include <stdio.h>

int
check_table_sig(const char *table_path)
{
	return TRUE;
}

TableHeader *
get_table_header(const char *table_path)
{
	FILE *pf;
	int len = strlen(IM_TBL_SIG) + 1;
	char buf[20];
	TableHeader *header;

	pf = fopen(table_path, "r");
	if (!pf)
		return NULL;

	header = (TableHeader *)malloc(sizeof(TableHeader));
	memset(header, 0, sizeof(TableHeader));

	if (fread(buf, len, 1, pf) != 1) {
		fclose(pf);
		free(header);
		return NULL;
	}
	if (fread(header, sizeof(TableHeader), 1, pf) != 1) {
		fclose(pf);
		free(header);
		return NULL;
	}
	return header;
}

char *
get_table_cname(const char *table_path)
{
	TableHeader *header = get_table_header(table_path);
	char *cname;

	if (header) {
		cname = strdup(header->cname);
		free(header);
		return cname;
	}
	else {
		free(header);
		return NULL;
	}
}

char *
get_table_ename(const char *table_path)
{
	TableHeader *header = get_table_header(table_path);
	char *ename;

	if (header) {
		ename = strdup(header->ename);
		free(header);
		return ename;
	}
	else {
		free(header);
		return NULL;
	}
}

BinHeader *
get_bin_header(const char *bin_path)
{
	FILE *pf;
	int len = strlen(IM_TBL_SIG) + 1;
	char buf[20];
	BinHeader *header;

	pf = fopen(bin_path, "r");
	if (!pf)
		return NULL;

	header = (BinHeader *)malloc(sizeof(BinHeader));
	memset(header, 0, sizeof(BinHeader));

	if (fread(buf, len, 1, pf) != 1) {
		fclose(pf);
		free(header);
		return NULL;
	}
	if (fread(header, sizeof(BinHeader), 1, pf) != 1) {
		fclose(pf);
		free(header);
		return NULL;
	}
	return header;
}

char *
get_bin_ename(const char *bin_path)
{
	BinHeader *header = get_bin_header(bin_path);
	char *ename;

	if (header) {
		ename = strdup(header->ename);
		free(header);
		return ename;
	}
	else {
		free(header);
		return NULL;
	}
}

char *
get_table_dir(const char *table_path)
{
	char *buf = strdup(table_path);
	int len = strlen(buf);
	char *slash;

	if (!len)
		return NULL;

	slash = buf + len -1;
	while (*slash != '/')
		slash--;
	*slash = 0x0;
	return buf;
}

char *
get_table_id(const char *table_path)
{
	return NULL;
}

// vi:ts=4:nowrap:cin:expandtab
